﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace TestGame {
    /// <summary>
    /// Used in Helping pack and unpack bytes into their associated arrays.
    /// </summary>
    class NetworkHelper {

        /// <summary>
        /// Constructs a packet with a specific header, and converts the given 
        /// parameters into a byte array. Takes care of Host and Network order.
        /// </summary>
        /// <param name="seed"></param>
        /// <returns></returns>
        public static byte[] PackHandshakeHeader(int seed) {
            // Packet: Seed (4 bytes) 
            int convertSeed = IPAddress.HostToNetworkOrder(seed);
            byte[] seedBytes = BitConverter.GetBytes(convertSeed);


            return seedBytes;
        }

        /// <summary>
        /// Unpacks the byte array into their associated variables. If an error
        /// occurs, the method returns false. An error can be too many or too 
        /// little variables given. Takes care of Host and Network order.
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="seed"></param>
        /// <returns>True if all data is correct, false if not</returns>
        public static bool UnpackHandshakeHeader(byte[] buffer, out int seed) {
            int uncovertedSeed = BitConverter.ToInt32(buffer, 0);
            seed = IPAddress.NetworkToHostOrder(uncovertedSeed);

            return true;
        }
    }
}
